import React        from 'react';
import { Link }     from 'react-router-dom';
import MUtil        from 'util/mm.jsx'
import  accompanying  from 'service/accompanying-service.jsx'
import PageTitle    from 'component/page-title/index.jsx';
import TableList    from 'util/table-list/eva-tableList.jsx';
import Pagination   from 'util/pagination/index.jsx';//用户列表分页
import ListSearch   from './index-list-search.jsx';
import './index.scss';
const _mm   = new MUtil();
const _acc = new accompanying();


class Evalation extends React.Component{
   
 constructor(props){
        super(props);
        this.state = {
            list            : [],
            pageNum         : 1,
            startTime:    '',
             endTime:  '',
             searchKeyword   : ''
        };
    
    }
 componentDidMount(){
       

    }



    loadUserList(){
/*时间转换*/
   /*   var d = new Date(this.state.startTime);  
       var  starttime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' 0:0:00' ; 

     var d2 = new Date(this.state.endTime);  
       var  endtime = d2.getFullYear() + '-' + (d2.getMonth() + 1) + '-' + d2.getDate() + ' 23:59:59' ; */
      
        let listParam = {};
        //listPaam赋值 ,请求服务器,关键字要和服务器匹配，不然服务器取不到值
             listParam.userId  = this.state.searchKeyword;
             listParam.pageNum  = this.state.pageNum;
             listParam.startTime   =this.state. startTime;
             listParam.endTime  =this.state.endTime;
   
        
        // 请求接口
       _acc.getevaList(listParam).then(res => {
            this.setState(res);
        }, errMsg => {
            this.setState({
                list : []
            });
            _mm.errorTips(errMsg);
        });

   
    }

    // 页数发生变化的时候
    onPageNumChange(pageNum){
        this.setState({
            pageNum : pageNum
        }, () => {
            this.loadUserList();
        });
    }

     // 搜索
    onSearch(startDate,twoData,searchKeyword){console.log(searchKeyword);

       if(searchKeyword===''){ /*不能写null,坑*/
           _mm.errorTips("商品ID或用户ID，不能为空");
           }
     else{

        this.setState({ //设置值
            pageNum         : 1,
            searchKeyword   : searchKeyword,
            startTime:startDate,
            endTime:twoData
        }, () => {
            this.loadUserList();
             });
    }
  }
//删除整个发布内容
    onDel(evaId){
     
  // 请求接口
      _acc.del(evaId).then(
        res => {
            _mm.successTips(res);
             this.loadUserList();
        }, 
        errMsg => {
            _mm.errorTips(errMsg);
        });


     }

     //删除单个
    onimg(imgID,stringname){
     
  // 请求接口
       _acc.delimg(imgID,stringname).then(
        res => {
            _mm.successTips(res);
             this.loadUserList();
        }, 
        errMsg => {
            _mm.errorTips(errMsg);
        });


     }
   

    render(){
          let listBody = this.state.list.map((acc, index) => {
            return (
                
                <tr key={index}>
                 
                    <td>{acc.userId}</td>
                    <td>{
                       acc.content!=''?<div>{acc.content}</div>
                       :<div>没有内容</div>
                       }
                    </td>
                    <td> 
                            {  
                               /* 一定要做空判断，如果为null,会显示空白，报错。0表示不为空 以下注释为用户上传了多张图片，目前只规定用户只上传一张图片*/
                             /*   acc.listImgIsNull==0?
                                   acc.listimg.map(item => {
                                        return (
                                             <div align="center" >
                                        <a href={`${acc.ossimgHost}${item}`} target="_blank">
                                              
                                                    <img className="p-img"  alt={acc.evaId}
                                                        src={`${acc.ossimgHost}${item}`}/>
                                         
                                                </a>
                                                   &nbsp;  
                                                   <a className="opear" 
                                               onClick={(e) => this.onimg(acc.id,item)}>删除上面的图片</a>
                                                 &nbsp;  
                                              </div>

                                            
                                        );
                                    }) : <div>没有图片或者视频</div>*/

                                    acc.listImgIsNull==0?
                                
                                             <div align="center" >
                                        <a href={`${acc.ossimgHost}${acc.fileName}`} target="_blank">
                                              
                                                    <img className="p-img"  alt={acc.evaId}
                                                        src={`${acc.ossimgHost}${acc.fileName}`}/>
                                         
                                                </a>
                                                   &nbsp;  
                                                   <a className="opear" 
                                               onClick={(e) => this.onimg(acc.id,acc.fileName)}>删除上面的图片</a>
                                                 &nbsp;  
                                              </div>
                                      
                                     : <div>没有图片</div>

                           }
                    </td>
                      <td>{
                       acc.voideoName!=null?<div>{acc.ossimgHost+acc.voideoName}
                                &nbsp;    &nbsp;    &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  
                                                   <a className="opear" 
                                               onClick={(e) => this.onimg(acc.id,acc.voideoName)}>删除此视频</a>
                                                 &nbsp;  
                       </div>
                       :<div>没有视频</div>
                       }
                    </td>
                       
                    <td>{acc.createTime}</td>       
                    <td>
                        <a className="opear"
                            onClick={(e) => this.onDel(acc.itemId)}>删除全部</a>
                    </td> 

                    </tr>
                    
                    );
                    });

             return (
            
                <div id="page-wrapper">
                  <PageTitle title="用户动态"/>
              <ListSearch ch={(startDate,twoData,searchKeyword) => 
                    {this.onSearch(startDate,twoData,searchKeyword)}}/>

                <TableList tableHeads={['用户名', '内容', '所有图片', '视频地址','发布时间','操作']}>
                 {listBody}
                </TableList>
                <Pagination current={this.state.pageNum} //当前页数
                    total={this.state.total}  //共多少页
                    //跳转
                    onChange={(pageNum) => this.onPageNumChange(pageNum)}/>
                  </div>


        );



    }
}

export default Evalation;